<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/13
 * Time: 9:17
 */
namespace app\reserve\validate;

use think\Db;
use think\Validate;

class Adminaction extends Validate{

    protected $rule = [
        'action_id'                     => ['require','number'],
        'model'                         => ['require'],
        'method'                        => ['require'],
        'action_name'                   => ['require','checkAction'],
        'action_name_en'                => ['require'],
        'group_name'                    => ['require'],
        'pagenum'                       => ['require'],
        'listNum'                       => ['require'],
    ];

    protected $message = [
        'action_id'                     => 'id不能为空',
        'model'                         => '控制器名不能为空',
        'method'                        => '方法名不能为空',
        'action_name'                   => '权限名称参数错误',
        'action_name_en'                => '权限英文名称参数错误',
        'group_name'                    => '组名称不能为空',
        'pagenum'                       => '页数不能为空',
        'listNum'                       => '条数不能为空',
    ];

    protected $scene = [
        //  添加权限
        'addAction'                     => ['action_name','action_name'],
        // 编辑权限
        'editAction'                    => ['action_id','action_name'=>['require','checkAction2']],
        // 权限列表
        'selectAction'                  => ['pagenum','listNum'],
    ];

    // 添加时验证权限名称的唯一性
    protected function checkAction($value)
    {
        $where_add['action_name'] = $value;
        $where_add['status']      = array("NEQ",9);
        $res = Db::name('admin_action')->where($where_add)->value("action_id");
        return $res ? false : true;
    }

    // 修改时验证权限名称的唯一性
    public function checkAction2($value,$rule,$data)        //  第三个参数是参数包
    {
        $where['action_name'] = $value;
        $where['action_id']   = array("NEQ",$data['action_id']);
        $res = Db::name('admin_action')->where($where)->value("action_id");
        return $res ? false : true;
    }
}